from django.http import JsonResponse
from django.test import TestCase
from django.views.decorators.csrf import csrf_exempt

from wepapi.models import UserInfo


# Create your tests here.

@csrf_exempt
def user_register(request):
    if request.method == 'POST':
        name = request.POST.get("name")
        pwd = request.POST.get("pwd")
        if name and pwd:

            response_data = {
                'code': 200,
                'msg': "注册成功",
                'data': {
                    'name': name,
                    'pwd': pwd
                }
            }
            UserInfo.objects.create(name=name, pwd=pwd)
            return JsonResponse(response_data)
        else:
            response_data = {
                'code': 400,
                'msg': "失败，缺少必要信息",
                'data': {}
            }
            return JsonResponse(response_data, status=400)
    else:
        # 如果不是POST请求，可以返回错误信息或者返回表单页面
        response_data = {
            'code': 405,
            'msg': "请求方法不允许",
            'data': {}
        }
        return JsonResponse(response_data, status=405)

